package com.ning.parking.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ning.parking.config.GlobalVariableUtils;
import com.ning.parking.domain.ParkingLotBaseInfo;
import com.ning.parking.domain.ParkingLotDrivewayInfo;
import com.ning.parking.domain.VO.Pagination;
import com.ning.parking.domain.dto.ParkingLotBaseInfoAddOrUpdateDTO;
import com.ning.parking.domain.dto.ParkingLotBaseInfoDto;
import com.ning.parking.service.ParkingLotBaseInfoService;
import com.ning.parking.service.ParkingLotDrivewayInfoService;
import com.scsonic.common.domain.parklot.ParkingLotBaseInfoVO;
import com.scsonic.common.enums.parklot.DrivewayTypeEnum;
import com.scsonic.framework.common.response.R;
import com.scsonic.framework.exception.BizException;
import com.scsonic.framework.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ChenN
 * @Date 2025/3/7 15:32
 * @Version 0.0.1
 **/
@Slf4j
@RestController
@RequestMapping("parkingLot")
public class ParkingLotController {

    @Autowired
    private ParkingLotBaseInfoService parkingLotBaseInfoService;
    @Autowired
    private ParkingLotDrivewayInfoService parkingLotDrivewayInfoService;

    /**
     * ----------------------------
     * @param parkingLotBaseInfoDTO
     * @return
     */
    @PostMapping("save")
    public R<Long> save(@RequestBody ParkingLotBaseInfoAddOrUpdateDTO parkingLotBaseInfoDTO) {
        ParkingLotBaseInfoVO save = parkingLotBaseInfoService.save(parkingLotBaseInfoDTO);
        return R.ok(save.getId());
    }

    @PostMapping("saveParkingLotBaseInfo")
    public R<Long> saveParkingLotBaseInfo(@RequestBody ParkingLotBaseInfo parkingLotBaseInfo) {
        ParkingLotBaseInfo parkingUserInfo1 = parkingLotBaseInfoService.saveParkingLotBaseInfo(parkingLotBaseInfo);
        return R.ok(parkingUserInfo1.getParkingLotId());
    }

    @GetMapping("byId")
    public R<ParkingLotBaseInfo> byId(ParkingLotBaseInfo parkingLotId) {
        if (parkingLotId.getParkingLotId() == null){
            throw new BizException("ParkingLotId is null ");
        }
        ParkingLotBaseInfo parkingLotBaseInfo = parkingLotBaseInfoService.getById(parkingLotId.getParkingLotId());
        parkingLotBaseInfo.setParkingLotDrivewayInfos(parkingLotDrivewayInfoService.lambdaQuery().eq(ParkingLotDrivewayInfo::getParkingLotId,parkingLotId.getParkingLotId()).list());
        return R.ok(parkingLotBaseInfo);
    }

    @GetMapping("get-parking-lot-base-info-list")
    public R<List<ParkingLotBaseInfo>> getParkingLotBaseInfoList() {
        return R.ok(parkingLotBaseInfoService.lambdaQuery().list());
    }

    @GetMapping("get-parking-lot-driveway-list-by-type")
    public R<List<ParkingLotDrivewayInfo>> getParkingLotBaseInfoList(Long parkingLotId, DrivewayTypeEnum drivewayType) {
        return R.ok(parkingLotDrivewayInfoService.lambdaQuery().eq(ParkingLotDrivewayInfo::getDrivewayType,drivewayType).eq(ParkingLotDrivewayInfo::getParentParkingLotId,parkingLotId).list());
    }

    @GetMapping("page")
    public R<Pagination<ParkingLotBaseInfo>> page(ParkingLotBaseInfoDto parkingLotBaseInfoDto) {
        Pagination<ParkingLotBaseInfo> page = parkingLotBaseInfoService.parkingLotBasePage(parkingLotBaseInfoDto);
        return R.ok(page);
    }


    @PostMapping("updParkingLotBaseInfo")
    public R<Boolean> updParkingLotBaseInfo(@RequestBody ParkingLotBaseInfo parkingLotBaseInfo) {
        if (parkingLotBaseInfo.getParkingLotId() == null){
            throw new BizException("ParkingLotId is null ");
        }
        parkingLotBaseInfo.setUpdateTime(DateUtils.date());
        parkingLotBaseInfo.setUpdateBy(GlobalVariableUtils.getUser().getUserName());
        boolean updateById = parkingLotBaseInfoService.updateById(parkingLotBaseInfo);
        if (updateById&& ObjectUtil.isNotEmpty(parkingLotBaseInfo.getParkingLotDrivewayInfos())){
            LambdaQueryWrapper<ParkingLotDrivewayInfo> wrapper = Wrappers.lambdaQuery(ParkingLotDrivewayInfo.class).eq(ParkingLotDrivewayInfo::getParkingLotId, parkingLotBaseInfo.getParkingLotId());
            parkingLotDrivewayInfoService.remove(wrapper);
            parkingLotDrivewayInfoService.saveBatch(parkingLotBaseInfo.getParkingLotDrivewayInfos());
        }
        return R.ok(updateById);
    }

}
